import pygame, random, sys
from pygame.locals import *

import functions
import classes
import collections


MAXENG = 201
MAXRESEARCHER = 251
MAXPILOT = 41
MAXTRAIN = 100
TRAININGTIME = 24

class player:
	def __init__(self):
		self.year = 3100
		self.tick = 0.0
		self.humanpool = 6000
		self.trainingresearchers = 0
		self.trainingengineers = 0
		self.trainingpilots = 0
		self.trainingresearch = False
		self.trainingengineer = False
		self.trainingpilot = False
		self.mastercontrol = False
		self.inventory = False
		self.depositanalysis = False
		self.galmap = False
		self.earthshuttle = False

		self.messages = classes.Messages()
		self.messages.addMsg(self.year, self.tick, 'Welcome to pyDeuteros')

		self.crews = False
		self.crewnames = classes.crewnames('data/names.dat')
		self.crews = []
		self.crewsintraining = []

		self.orbitals = []

		self.damagedbases = []
		self.damagedbases.append('themoon')

		self.proditems = []
		self.proditems.append('derrick')

		self.research = collections.OrderedDict()
		self.research['unknownitem'] = 'hidden'
		self.research['derrick'] = 'done'
		self.research['schassis'] = 'waiting'
		self.research['sdrive'] = 'waiting'
		self.research['mehfuel'] = 'waiting'

		self.shuttlebays = []
		self.shuttlebays.append('shuttle1')
		self.shuttlebays.append('shuttle2')
		self.shuttlebays.append('shuttle3')
		self.shuttlebays.append('shuttle4')

		self.spacebays = []
		self.spacebays.append('spacebay1')
		self.spacebays.append('spacebay2')
		self.spacebays.append('spacebay3')
		self.spacebays.append('spacebay4')

		self.rightclick = { 'screen': 'earthcity',
				    'subscreen': 'main',
				  }

	def rightclickgoeswhere(self):
		return(self.rightclick['screen'], self.rightclick['subscreen'])

	def updaterightclick(self, screen, subscreen):
		self.rightclick = { 'screen': screen,
				    'subscreen': subscreen,
				  }

	def unreadMsgs(self):
		return(self.messages.Unread())

	def returnTrainingStatus(self):
		return(self.humanpool, self.trainingresearchers, self.trainingengineers, self.trainingpilots, self.trainingresearch, self.trainingengineer, self.trainingpilot)

	def check_crew_topup(self, crewtype):
		self.crewToTrain = MAXTRAIN
		self.crewVacancies = 0
		for self.crew in self.crews:
			self.leadername, self.crewtype, self.numberofcrew, self.location, self.sublocation, self.rank = self.crew.getdetails()
			if (self.crewtype == crewtype) and (self.location == 'earthcity'):
				if (crewtype == 'researcher'):
					if (self.numberofcrew >= MAXRESEARCHER):
						self.crewToTrain = 0
					else:
						self.crewVacancies = MAXRESEARCHER - self.numberofcrew
				elif (crewtype == 'engineer'):
					if (self.numberofcrew >= MAXENG):
						self.crewToTrain = 0
					else:
						self.crewVacancies = MAXENG - self.numberofcrew
				break
		if (self.crewVacancies > 0) and (self.crewVacancies < MAXTRAIN):
			self.crewToTrain = self.crewVacancies
		return(self.crewToTrain)			

	def add_rem_training(self, action):
		if (self.humanpool > 0):
			if not (self.trainingresearch):
				if (action == 'rresearchers'):
					if (self.trainingresearchers > 0):
						self.trainingresearchers -= 1
						self.humanpool += 1
				elif (action == 'aresearchers'):
					self.maxresearchers = self.check_crew_topup('researcher')
					if (self.trainingresearchers < self.maxresearchers):
						self.trainingresearchers += 1
						self.humanpool -= 1
			if not (self.trainingengineer):
				if (action == 'rengineers'):
					if (self.trainingengineers > 0):
						self.trainingengineers -= 1
						self.humanpool += 1
				elif (action == 'aengineers'):
					self.maxengs = self.check_crew_topup('engineer')
					if (self.trainingengineers < self.maxengs):
						self.trainingengineers += 1
						self.humanpool -= 1
			if not (self.trainingpilot):
				if (action == 'rpilots'):
					if (self.trainingpilots > 0):
						self.trainingpilots -= 1
						self.humanpool += 1
				elif (action == 'apilots'):
					if (self.trainingpilots < MAXPILOT):
						self.trainingpilots += 1
						self.humanpool -= 1

	def dotraining(self):
		if not (self.trainingresearch):
			if (self.trainingresearchers > 0):
				for self.crew in self.crews:
					self.leadername, self.crewtype, self.numberofcrew, self.location, self.sublocation, self.rank = self.crew.getdetails()
					if (self.crewtype == 'researcher'):
						self.crew.doTopUp(self.trainingresearchers, TRAININGTIME)
						self.crewsintraining.append(self.crew)
						self.trainingresearch = True
						break
				if not (self.trainingresearch):
					self.trainingresearch = True
					self.crewsintraining.append(classes.crew(self.crewnames.getname(), 'researcher', self.trainingresearchers, TRAININGTIME))
		if not (self.trainingengineer):
			if (self.trainingengineers > 0):
				for self.crew in self.crews:
					self.leadername, self.crewtype, self.numberofcrew, self.location, self.sublocation, self.rank = self.crew.getdetails()
					if (self.crewtype == 'engineer') and (self.location == 'earthcity') and (self.sublocation == 'production'):
						self.crew.doTopUp(self.trainingengineers, TRAININGTIME)
						self.crewsintraining.append(self.crew)
						self.trainingengineer = True
						break
				if not (self.trainingengineer):
					self.trainingengineer = True
					self.crewsintraining.append(classes.crew(self.crewnames.getname(), 'engineer', self.trainingengineers, TRAININGTIME))
		if not (self.trainingpilot):
			if (self.trainingpilots > 0):
				self.trainingpilot = True
				self.crewsintraining.append(classes.crew(self.crewnames.getname(), 'pilot', self.trainingpilots, TRAININGTIME))
		for self.trainingcrew in self.crewsintraining:
			self.remaining = self.trainingcrew.dotraining()
			if (self.remaining == 0):
				self.leadername, self.crewtype, self.numberofcrew, self.location, self.sublocation, self.rank = self.trainingcrew.getdetails()
				if (self.crewtype == 'researcher'):
					self.trainingresearch = False
					self.trainingresearchers = 0
					self.trainingcrew.setLocation('earthcity', 'research')
				elif (self.crewtype == 'engineer'):
					self.trainingengineer = False
					self.trainingengineers = 0
					self.trainingcrew.setLocation('earthcity', 'production')
				elif (self.crewtype == 'pilot'):
					self.bayfree, self.FreeBayName = self.NextFreeCrewBay('earthcity', 'shuttlebay')
					if (self.bayfree):
						self.leadername, self.crewtype, self.numberofcrew, self.location, self.sublocation, self.rank = self.trainingcrew.getdetails()
						self.messages.addMsg(self.year, self.tick, self.rank + ' ' + self.leadername + ' completed training')
						self.trainingpilot = False
						self.trainingpilots = 0
						self.trainingcrew.setLocation('earthcity', self.FreeBayName)
						self.crews.append(self.trainingcrew)
						self.crewsintraining.remove(self.trainingcrew)
				self.leadername, self.crewtype, self.numberofcrew, self.location, self.sublocation, self.rank = self.trainingcrew.getdetails()
				if ((self.crewtype == 'researcher') or (self.crewtype == 'engineer')) and (self.location == 'earthcity') and ((self.sublocation == 'research') or (self.sublocation == 'production')):
					self.crewTopUp = False
					for self.crew in self.crews:
						if (self.trainingcrew == self.crew):
							self.crewTopUp = True
							break
					if not (self.crewTopUp):
						self.crews.append(self.trainingcrew)
						self.messages.addMsg(self.year, self.tick, self.rank + ' ' + self.leadername + ' completed training')
					self.crewsintraining.remove(self.trainingcrew)
		
	def crewSearch(self, crewtype, location, sublocation):
		self.crewfound = False
		self.crew = False
		self.leadername = False 
		self.crewtype = False
		self.numberofcrew = False
		self.location = False
		self.sublocation = False
		self.rank = False
		for self.crew in self.crews:
			self.leadername, self.crewtype, self.numberofcrew, self.location, self.sublocation, self.rank = self.crew.getdetails()
			if  (self.crewtype == crewtype) and (self.location == location) and (self.sublocation == sublocation):
				self.crewfound = True
				break
		return(self.crewfound, self.crew, self.leadername, self.rank, self.numberofcrew)

	def NextFreeCrewBay(self, location, baytype):
		self.bayfree = False
		self.FreeBayName = False
		if (baytype == 'shuttlebay'):
			self.bays = list(self.shuttlebays)
		else:
			self.bays = list(self.spacebays)
		for self.crew in self.crews:
			self.leadername, self.crewtype, self.numberofcrew, self.location, self.sublocation, self.rank = self.crew.getdetails()
			for self.bay in self.bays:
				if (self.location == location) and (self.sublocation == self.bay):
					self.bays.remove(self.bay)
					break
			if (len(self.bays) <= 0):
				break
		if (len(self.bays) > 0):
			self.FreeBayName = self.bays.pop(0)
			self.bayfree = True
		return(self.bayfree, self.FreeBayName)

	def moveProdShuttleBay(self, location):
		self.crewfoundprod, self.crewprod, self.leadernameprod, self.rankprod, self.numberofcrewprod = self.crewSearch('engineer', location, 'production')
		if (self.crewfoundprod):
			self.bayfree, self.FreeBayName = self.NextFreeCrewBay(location, 'shuttlebay')
			if (self.bayfree):
				self.crewprod.setLocation(location, self.FreeBayName)

	def moveProdtoProd(self, location, bayname):
		self.crewfoundprod, self.crewprod, self.leadernameprod, self.rankprod, self.numberofcrewprod = self.crewSearch('engineer', location, 'production')
		if not (self.crewfoundprod):
			self.crewfoundprod, self.crewprod, self.leadernameprod, self.rankprod, self.numberofcrewprod = self.crewSearch('engineer', location, bayname)
			self.crewprod.setLocation(location, 'production')

	def getProdItems(self):
		return(self.proditems)

	def getResearch(self):
		return(self.research)

	def setResearchStatus(self, item, status):
		self.research[item] = status

	def addResearch(self, item):
		self.research[item] = 'waiting'

	def addProdItem(self, item):
		self.proditems.append(item)

	def doCrew(self):
		for self.crew in self.crews:
			self.crew.dotick()
			if (self.crew.killsomecrew()):
				self.leadername, self.crewtype, self.numberofcrew, self.location, self.sublocation, self.rank = self.crew.getdetails()
				self.messages.addMsg(self.year, self.tick, self.rank + ' ' + self.leadername + ' has retired')
				self.crews.remove(self.crew)

	def dotick(self, player, iteminfo, earthcity):
		self.dotraining()
		self.doCrew()
		earthcity.doTick(player, iteminfo)
		self.tick += 1
		if (self.tick > 365):
			self.year += 1
			self.tick = 0.0
		return(self.year, self.tick)


